package com.grm.sys.service.impl;

import com.grm.common.exception.BusinessException;
import com.grm.sys.mapper.SysDictDataMapper;
import com.grm.sys.mapper.SysDictTypeMapper;
import com.grm.sys.model.SysDictType;
import com.grm.sys.request.dict.type.SysDictTypeAddParam;
import com.grm.sys.request.dict.type.SysDictTypeQueryParam;
import com.grm.sys.request.dict.type.SysDictTypeUpdateParam;
import com.grm.sys.service.SysDictTypeService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<SysDictType> querySelective(SysDictTypeQueryParam queryParam) {
        return sysDictTypeMapper.queryList(queryParam);
    }

    @Override
    public void update(SysDictTypeUpdateParam updateParam) {
        SysDictType dbSysDictType = sysDictTypeMapper.queryById(updateParam.getId());
        if (dbSysDictType == null) {
            throw new BusinessException("枚举类型不存在！");
        }
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(updateParam, sysDictType);
        sysDictTypeMapper.update(sysDictType);
    }

    @Override
    public void add(SysDictTypeAddParam addParam) {
        SysDictType dbSysDictType = sysDictTypeMapper.queryByType(addParam.getType());
        if (dbSysDictType != null) {
            throw new BusinessException("枚举类型已存在！");
        }
        // copyBean
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(addParam, sysDictType);
        sysDictTypeMapper.add(sysDictType);
    }

    @Override
    public void delete(List<Long> ids) {
        List<String> types = sysDictTypeMapper.queryTypesByIds(ids);
        if(ObjectUtils.isNotEmpty(types)){
            sysDictDataMapper.deleteByTypes(types);
        }
        sysDictTypeMapper.deleteByIds(ids);
    }
}
